# -*- coding: utf-8 -*-

from openerp import fields, models, api

class stock_picking(models.Model):
    _inherit = 'stock.picking'

    carrier_partner_id = fields.Many2one('res.partner', u'承运方司机')
    car_number=fields.Char(string="车牌号")
    do_man=fields.Many2one('res.users',string=u"操作者",default=lambda self: self.env.user,states={'done': [('readonly', True)]})


    car_qty=fields.Integer(string=u"车数")
    street_id=fields.Char(string=u"街道", size=24)
    city_id = fields.Many2one('res.country.state.city', string=u'城市')
    state_id=fields.Many2one("res.country.state", string=u"省", ondelete='restrict')
    country_id=fields.Many2one('res.country', string=u"国家",ondelete='restrict',default=49)
    date_arrive=fields.Datetime(string=u"送达时间")
    receiver_id=fields.Many2one('res.partner',string=u"收货人")
    content=fields.Text(string=u"备注")

class stock_location(models.Model):
    _inherit = 'stock.location'

    _rec_name = 'name'


class stock_move(models.Model):
    _inherit = 'stock.move'


    def action_confirm(self, cr, uid, ids, context=None):
       
        procs_to_check = []
        for move in self.browse(cr, uid, ids, context=context):
            if move.procurement_id and move.procurement_id.sale_line_id:
                procs_to_check += [move.procurement_id]
        res = super(stock_move, self).action_confirm(cr, uid, ids, context=context)
        
        pick_obj = self.pool.get("stock.picking")
        for proc in procs_to_check:
            pickings = list(set([x.picking_id.id for x in proc.move_ids if x.picking_id]))
            if pickings:
                pick_obj.write(cr, uid, pickings, {
                    'car_qty': proc.sale_line_id.order_id.car_qty,
                    'street_id': proc.sale_line_id.order_id.street_id,
                    'city_id': proc.sale_line_id.order_id.city_id.id,
                    'state_id': proc.sale_line_id.order_id.state_id.id,
                    'country_id': proc.sale_line_id.order_id.country_id.id,
                    'date_arrive': proc.sale_line_id.order_id.date_arrive,
                    'receiver_id': proc.sale_line_id.order_id.receiver_id.id,
                    'content': proc.sale_line_id.order_id.content
                    }, context=context)
        
        return res


class stock_quant(models.Model):
    _inherit = 'stock.quant'

    product_uos_mmm=fields.Float(compute='_compute_product_uos_mmm')
    product_uos_out_qty = fields.Float(compute='_compute_product_uos_out_qty')
    product_uos_in_qty = fields.Float(compute='_compute_product_uos_in_qty')
    product_uos_previous_qty= fields.Float(compute='_compute_product_uos_previous_qty')
    product_uos_current_qty= fields.Float(compute='_compute_product_uos_current_qty')
    attribute_1= fields.Many2one('product.attribute.value', string=u'属性1',store=True, 
        related="product_id.attribute_1")
    attribute_2= fields.Many2one('product.attribute.value', string=u'属性2',store=True,
        related="product_id.attribute_2")
    attribute_3= fields.Many2one('product.attribute.value', string=u'属性3',store=True, 
        related="product_id.attribute_3")
    attribute_4= fields.Many2one('product.attribute.value', string=u'属性4',store=True,
        related="product_id.attribute_4")
    attribute_5= fields.Many2one('product.attribute.value', string=u'属性5',store=True, 
        related="product_id.attribute_5")

    sale_uom_qty=fields.Float(string="订单数量")
    sale_uos_qty=fields.Float()
    mrp_uom_qty=fields.Float(string="订单入库")
    mrp_uos_qty=fields.Float()
    out_uom_qty=fields.Float(string="订单出库")
    out_uos_qty=fields.Float()
    current_uom_qty=fields.Float(string="订单结存")
    current_uos_qty=fields.Float()

    @api.one
    def _update_in_out(self,start_date,end_date,move=False):
        """更新出入库数据"""
        if self.product_id.sale_ok==True:
            print(self.product_id,self.product_id.name)
            last_move =super(stock_quant,self)._update_in_out(start_date,end_date,move)
            order_line_ids = self.env['sale.order.line'].search([
                ('product_id','=',self.product_id.id),
                ('order_id.project_id.project.location_id','=',self.location_id.id),
                ('state','=','confirmed')
                ])
            print('>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>')
            print(order_line_ids)
            self.sale_uom_qty = sum(ordr_line.product_uom_qty for ordr_line in order_line_ids)
            self.sale_uos_qty = sum(ordr_line.product_uos_qty for ordr_line in order_line_ids)
            sale_project_id = self.env['sale.project'].search([('location_id','=',self.location_id.id)])
            print(sale_project_id)
            mrp_product_ids=self.env['mrp.production'].search([
                ('product_id','=',self.product_id.id),
                ('project_id','=',sale_project_id.id)])
            print(mrp_product_ids)

            mrp_uom_qty = 0
            for mrp_line in mrp_product_ids:
                for move in mrp_line.move_created_ids2:
                    if move.location_id.id == 7 : #成品数量
                        mrp_uom_qty += move.product_uom_qty
                    scrap_move_ids = self.env['stock.move'].search([
                        ('product_id','=',self.product_id.id),
                        ('location_dest_id','=',4),#损耗库位
                        ('location_id','=',move.location_dest_id.id),#质检库
                        ('state','=','done')])
                    for scrap_move in scrap_move_ids: # 损耗数量
                        mrp_uom_qty -= move.product_uom_qty
            self.mrp_uom_qty = mrp_uom_qty
            if self.product_uos_mmm!=0:
                self.mrp_uos_qty = mrp_uom_qty / self.product_uos_mmm
            stock_out_ids=self.env['stock.move'].search([
                ('product_id','=',self.product_id.id),
                ('location_dest_id','=',self.location_id.id),
                ('state','=','done')])
            self.out_uom_qty = sum(stock_out_line.product_uom_qty for stock_out_line in stock_out_ids)
            self.out_uos_qty = sum(stock_out_line.product_uos_qty for stock_out_line in stock_out_ids)
            self.current_uom_qty=self.sale_uom_qty-self.out_uom_qty
            self.current_uos_qty=self.sale_uos_qty-self.out_uos_qty
            print(self.sale_uom_qty,self.current_uos_qty)
            return last_move
    @api.one
    @api.depends('product_id.uos_id')
    def _compute_product_uos_mmm(self):
        print(self.product_id.uos_id)
        self.product_id.uos_id._compute_mmm()
        self.product_uos_mmm= self.product_id.uos_id.mmm

    @api.one
    @api.depends('product_uom_out_qty')
    def _compute_product_uos_out_qty(self):
        if self.product_uos_mmm!=0:
            self.product_uos_out_qty = self.product_uom_out_qty / self.product_uos_mmm

    @api.one
    @api.depends('product_uom_in_qty')
    def _compute_product_uos_in_qty(self):
        if self.product_uos_mmm!=0:
            self.product_uos_in_qty = self.product_uom_in_qty / self.product_uos_mmm

    @api.one
    @api.depends('product_uom_previous_qty')
    def _compute_product_uos_previous_qty(self):
        if self.product_uos_mmm!=0:
            self.product_uos_previous_qty = self.product_uom_previous_qty / self.product_uos_mmm

    @api.one
    @api.depends('product_uom_current_qty')
    def _compute_product_uos_current_qty(self):
        if self.product_uos_mmm!=0:
            self.product_uos_current_qty = self.product_uom_current_qty / self.product_uos_mmm